package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.pojo.Role;
import cn.zj.logistics.pojo.RoleExample;
import cn.zj.logistics.pojo.RoleExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.RoleService;
import cn.zj.logistics.service.UserService;

@Controller
@RequestMapping("/role")
public class RoleController {
	
	
	@Autowired
	private RoleService roleService;
	
	
	@Autowired
	private UserService userService;
	

	// 跳转到 role 管理页面
	@RequiresPermissions("role:rolePage")
	@RequestMapping("/rolePage")
	public String rolePage() {
		return "rolePage";
	}
	
	
	
	@ResponseBody
	@RequiresPermissions("role:update")
	@RequestMapping("/update")
	public MessageObject update(Role role) {
		
		
		int row = roleService.updateByPrimaryKeySelective(role);
		
		MessageObject mo = MessageObject.creatMO(0, "修改数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "修改数据成功");
		}
		
		return mo;
		
	}
	
	
	@ResponseBody
	@RequiresPermissions("role:insert")
	@RequestMapping("/insert")
	public MessageObject insert(Role role) {
		
		int row = roleService.insert(role);
		
		MessageObject mo = MessageObject.creatMO(0, "新增数据失败，请联系管理员");
		if(row == 1) {
			mo = MessageObject.creatMO(1, "新增数据成功");
		}
		
		return mo;
		
	}
	
	
	/*
	 * jackson+@ResponseBody 方法直接返回json格式的数据
	 * 
	 */
	
	@ResponseBody
	@RequiresPermissions("role:list")
	@RequestMapping("/list")
	public PageInfo<Role> list(@RequestParam(defaultValue = "1")Integer pageNum,
								@RequestParam(defaultValue = "10")Integer pageSize,
								String keyword) {
		PageHelper.startPage(pageNum, pageSize);

		RoleExample example = new RoleExample();

		//开始条件查询
		if(StringUtils.isNotBlank(keyword)) {
			
			Criteria criteria1 = example.createCriteria();
			criteria1.andRolenameLike("%"+keyword+"%");
			
		}
		
		
		List<Role> roles = roleService.selectByExample(example);

		PageInfo<Role> pageInfo = new PageInfo<>(roles);
		
		return pageInfo;
	}
	
	
	@ResponseBody
	@RequiresPermissions("role:delete")
	@RequestMapping("/delete")
	public MessageObject delete(Long roleId) {
		
		/*
		 * 判断当前角色在用户表中是否有还有关联用户
		 * 有：不能删除
		 * 没有:可以删除
		 * 
		 */
		
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		
		criteria.andRoleIdEqualTo(roleId);
		//当前角色对应的用户
		List<User> users = userService.selectByExample(example);
		
		if(users.size() > 0) {
			MessageObject mo = MessageObject.creatMO(0, "当前角色关联有管理员，不能删除");
			return mo;
		}
		
		
		
		int row = roleService.deleteByPrimaryKey(roleId);
		
		MessageObject mo = MessageObject.creatMO(0, "删除数据失败，请联系管理员！！");
		
		//删除成功
		if(row == 1) {
			mo = MessageObject.creatMO(1, "删除数据成功！！！");
		}
		
		return mo;
	}
	
	
	
	
	/*
	 * 新增或者修改跳转的编辑页面
	 * 
	 */
	
	@RequestMapping("/roleEdit")
	public String roleEdit(Model m,Long roleId) {
		
		
		/*
		 * 如果是修改，有roleId，要根据id查询出对应的管理员并共享让jsp页面回显
		 */
		if(roleId != null) {
			Role role = roleService.selectByPrimaryKey(roleId);
			m.addAttribute("role", role);
		}
		
		
		
		return "roleEdit";
	}
	
	@RequestMapping("/checkRolename")
	@ResponseBody
	public boolean checkRolename(String rolename) {
		
		RoleExample example = new RoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andRolenameEqualTo(rolename);
		
		List<Role> roles = roleService.selectByExample(example );
		
		return roles.size() == 0 ? true : false;
	}
	
	
	
	
}
